<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Db;

class Company extends Base
{
	protected $id = 'company_id';
	protected $table = 'company';

    //列表
    public function index() {

        $where = $this->search();
        //列表
        $list = $this->joins()->where($where)->where('m.delete_time',$where['m.delete_time'])->order($this->id.' DESC')->fetchSql(0)->paginate(10,false,[
            'query' => input('param.')
        ]);
        $newList = [];
        foreach ($list as $k=>$v){
            $v['m_cid'] = md5($v['company_id'].'u78io90ku65hjl38');
            $newList[] = $v;
        }
        $this->assign('list',$newList);
        $this->assign('pager',$list->render());

        //弹出层搜索
        $this->_op();

        return $this->out();

    }

    public function department() {

        $id = input('param.id/d');
        $list = Db::name('CompanyDept')->where('company_id',$id)->select();

        return json($list);

    }

    //分析数据
    protected function parseData($data) {

        $where = [];
        $data['company_id'] && $where['company_id'] = ['<>',$data['company_id']];
        $data['account'] && $where['account'] = $data['account'];

        $repeat = Db::name($this->table)->where($where)->find();

        if($repeat) {
            return $this->error('该登录账号已经存在');
        }

        return $data;

    }


    protected function search() {

        $P = input('param.');
        $where = [];
        //公务只能看公务订单
        if( $this->user['official'] ){
            $where['quota'] = ['>',0];
        }
        $P['company_name'] && $where['company_name'] = ['like',"%{$P['company_name']}%"];
        $P['company_mode'] && $where['company_mode'] = $P['company_mode'];
        //性质
        $props = config('base.use_prop');
        $this->assign('props',TplOptions($props,$P['prop'],'name','id'));
        $P['prop'] && $where['prop'] = $P['prop'];
        //委托-只读单位
        if($P['entrust']) {
            $where['company_mode'] = 2;
        } elseif($P['own'] == 9) {
            $where['company_mode'] = 1;
        }


        //只查有订单的
        if($P['filter'] =='order') {
            $ids = Db::name('order')->distinct(true)->field('company_id')->column('company_id');
            $where['company_id'] = ['IN',$ids];
        }
        //弹出层搜索
        $op = input('param.op');
        if(in_array($op, ['search','bind','unbind']) ) {
            $this->assign('notPopup',0);
        }
        $this->assign('op',$op);
        return $where;
    }

    protected function options($item = []) {

        $company_modes = config('base.company_modes');
        $this->assign('company_modes',TplOptions($company_modes,$item['company_mode']));

        $prop = config('base.use_prop');
        $props = $item['prop'] ? explode(',',$item['prop']) : [];
        foreach($prop AS $i => $p) {
            $prop[$i]['open'] = true;
            if($props && in_array($p['id'],$props)) $prop[$i]['checked'] = true;
        }
        $this->assign('props',json_encode($prop));

        return $item;
    }
	
}
